﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CoyoEden.Providers;

namespace CoyoEden.Models
{
	/// <summary>
	/// A file in the portal.Maybe a pic in an album.
	/// </summary>
	[Serializable]
	public class PortalFile:BusinessBase<PortalFile,int>,IComparable<PortalFile>
	{
		#region .ctor
		public PortalFile() {
			base.Id = -1;
			_Name = "";
			_Url = "";
			_Hits = 0;

		}
		#endregion

		#region member variables
		private string _Name;
		public string Name {
			get {
				return _Name;
			}
			set { 
				if (_Name != value) {
					MarkChanged("Name");
					_Name = value;
				};
			}
		}
		private string _Url;
		/// <summary>
		/// App Relative web url.
		/// </summary>
		public string Url {
			get {
				return _Url ;
			}
			set {
				_Url = value;
			}
		}
		private int _Hits;
		public int Hits {
			get { return _Hits; }
			set {
				_Hits = value;
			}
		}
		private string _Description;
		/// <summary>
		/// 描述
		/// </summary>
		public string Description {
			get { return _Description; }
			set {
				if (_Description != value) {
					MarkChanged("Description");
					_Description = value;
				}
			}
		}
		/// <summary>
		/// File.Exists?Readonly
		/// </summary>
		public bool IsValid {
			get {
				return File.Exists(AbsoluteUrl);
			}
		}
		/// <summary>
		/// absolute url
		/// </summary>
		public string AbsoluteUrl {
			get {
				return Utils.ConvertToAbsolute(_Url).ToString();
			}
		}
		private static object _SyncRoot = new object();
		private static List<PortalFile> _PortalFiles;
		/// <summary>
		/// Gets an sorted list of all PortalFiles.
		/// </summary>
		public static List<PortalFile> PortalFiles {
			get {
				if (_PortalFiles == null) {
					lock (_SyncRoot) {
						if (_PortalFiles == null) {
							_PortalFiles = PortalService.FillPortalFiles();
						}
					}
				}
				return _PortalFiles;
			}
		}
		#endregion

		#region IComparable<PortalFile> Members

		public int CompareTo(PortalFile other) {
			return this.Id.CompareTo(other.Id);
		}

		#endregion

		#region BusinessBase implementation
		protected override void ValidationRules()
		{
			AddRule("Url","Url无效[文件路径为空]",UrlRule());
		}

		protected override PortalFile DataSelect(int id)
		{
			return PortalService.SelectPortalFile(id);
		}

		protected override void DataUpdate()
		{
			if (this.IsChanged)
				PortalService.UpdatePortalFile(this);
		}

		protected override void DataInsert()
		{
			if (this.IsNew) PortalService.InsertPortalFile(this);
		}

		protected override void DataDelete()
		{
			if (this.IsDeleted)
				PortalService.DeletePortalFile(this);
		}
		#endregion

		#region Methods
		public static PortalFile GetPortalFile(int id) {
			return PortalFiles.Find(obj=>obj.Id==id);
		}
		#endregion

		#region business rule
		private bool UrlRule() {
			var broken = false;
			if (string.IsNullOrEmpty(_Url))
			{
				broken = true;
			}
			return broken;
		}
		#endregion
	}
}
